//
//  HAAddressPopView.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HAAddressPopView.h"

@interface SYDZTalMenuColCell : UICollectionViewCell

/***/
@property (nonatomic, strong) UILabel *label;
/***/
@property (nonatomic, strong) NSString *name;

@end

@implementation SYDZTalMenuColCell

-(void)layoutSubviews {
    [super layoutSubviews];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}

-(void)setName:(NSString *)name {
    _name = name;
    self.label.text = _name;
}

- (UILabel *)label
{
    if (!_label) {
        _label = [UILabel ha_NLabelWithFont:14 textColor:@"#F03560" text:@"" alignment:1];
        [self.contentView addSubview:_label];
    }
    return _label;
}

@end

@interface SYDZTalMenuTabCell : UITableViewCell

/***/
@property (nonatomic, strong) UILabel *label;
/***/
@property (nonatomic, strong) NSString *name;
/***/
@property (nonatomic, assign) BOOL isSelected;

@end

@implementation SYDZTalMenuTabCell

-(void)layoutSubviews {
    [super layoutSubviews];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.centerY.equalTo(self.contentView);
    }];
}

-(void)setName:(NSString *)name {
    _name = name;
    self.label.text = _name;
}

-(void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    if (isSelected==YES) {
        _label.textColor = [UIColor colorWithHexString:@"#F03560"];
    }else{
        _label.textColor = [UIColor colorWithHexString:@"#373737"];
    }
}

- (UILabel *)label
{
    if (!_label) {
        _label = [UILabel ha_NLabelWithFont:14 textColor:@"#373737" text:@"" alignment:0];
        [self.contentView addSubview:_label];
    }
    return _label;
}

@end


@interface HAAddressPopView ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

/***/
@property (nonatomic, strong) UIView *topView1;
/***/
@property (nonatomic, strong) UIView *topView2;
/***/
@property (nonatomic, strong) UIView *lineView1;
/***/
@property (nonatomic, strong) UIView *lineView2;
/** 取消按钮 */
@property (nonatomic, strong) UIButton *cancelButton;
/***/
@property (nonatomic, strong) UILabel *titleLael1;
/***/
@property (nonatomic, strong) UILabel *titleLael2;

@property (nonatomic , strong) UICollectionView *collectionView;

@property (nonatomic, strong) UITableView *tableView;
/***/
@property (nonatomic, strong) NSMutableArray *showArray;
/***/
@property (nonatomic, strong) NSMutableArray *selectedArray;
/***/
@property (nonatomic, strong) NSString *province;
/***/
@property (nonatomic, strong) NSString *city;
/***/
@property (nonatomic, strong) NSString *area;
/**1.省 2.市 3.区*/
@property (nonatomic, assign) NSInteger showTpye;
/***/
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation HAAddressPopView

-(instancetype)initWithDZArray:(NSArray *)DZArray
{
    self = [super init];
    if (self) {
        
        self.dataArray = [NSArray arrayWithArray:DZArray];
        [self getData:self.dataArray];
        [self layoutViews];
    }
    return self;
}

-(void)getData:(NSArray*)array {
    
    [self.showArray addObjectsFromArray:array];
    
    [self.collectionView reloadData];
    [self.tableView reloadData];
    self.showTpye = 1;
}

#pragma mark - 点击事件
-(void)cancelButtonClicked {
    [self dismiss];
}

-(void)show {
    [super show];
}
-(void)dismiss {
    [super dismiss];
}


-(void)layoutViews {
    
    self.frame = CGRectMake(0, 0, kUIScreen_w, kUIScreen_h);
    [self addSubview:self.backgroundView];
    //顶部视图
    CGFloat alertViewH = kUIScreen_h *0.7;
    CGFloat alertViewW = kUIScreen_w;
    CGFloat alertViewX = 0;
    CGFloat alertViewY = kUIScreen_h-alertViewH;
    self.alertViewY = alertViewY;
    self.alertView.frame = CGRectMake(alertViewX, alertViewY, alertViewW, alertViewH);
    [self addSubview: self.alertView];
    
    [self.alertView addSubview:self.topView1];
    [self.topView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.alertView);
        make.height.equalTo(@50);
    }];
    
    [self.topView1 addSubview:self.cancelButton];
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.topView1);
        make.centerY.equalTo(self.topView1);
        make.height.equalTo(@44);
        make.width.equalTo(@44);
    }];
    
    [self.topView1 addSubview:self.titleLael1];
    [self.titleLael1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.topView1);
        make.left.equalTo(self.topView1).offset(15);
        make.right.equalTo(self.cancelButton.mas_left);
    }];
    
    [self.alertView addSubview:self.lineView1];
    [self.lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.alertView);
        make.top.equalTo(self.topView1.mas_bottom);
        make.height.equalTo(@1);
    }];
    
    [self.alertView addSubview:self.topView2];
    [self.topView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.alertView);
        make.top.equalTo(self.lineView1.mas_bottom);
        make.height.equalTo(@44);
    }];
    
    [self.topView2 addSubview:self.titleLael2];
    [self.titleLael2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@60);
        make.top.bottom.equalTo(self.topView2);
        make.left.equalTo(self.topView2).offset(15);
    }];
    
    [self.topView2 addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self.topView2);
        make.left.equalTo(self.titleLael2.mas_right);
    }];
    
    [self.alertView addSubview:self.lineView2];
    [self.lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.alertView);
        make.top.equalTo(self.topView2.mas_bottom);
        make.height.equalTo(@1);
    }];
    
    [self.alertView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView2.mas_bottom);
        make.left.right.equalTo(self.alertView);
        make.bottom.equalTo(self.alertView).offset(-app_bottomH);
    }];
}


#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.showArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SYDZTalMenuTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SYDZTalMenuTabCellID"];
    if (!cell) {
        cell = [[SYDZTalMenuTabCell alloc]initWithStyle:0 reuseIdentifier:@"SYDZTalMenuTabCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSString *name = @"";
    id mo = self.showArray[indexPath.row];
    if ([mo isKindOfClass:[HAAddressProvinceMo class]]) {
        HAAddressProvinceMo *provinceMo = (HAAddressProvinceMo *)mo;
        name = provinceMo.label;
    }else if ([mo isKindOfClass:[HAAddressCityMo class]]) {
        HAAddressCityMo *cityMo = (HAAddressCityMo *)mo;
        name = cityMo.label;
    }else if ([mo isKindOfClass:[HAAddressAreaMo class]]) {
        HAAddressAreaMo *areaMo = (HAAddressAreaMo *)mo;
        name = areaMo.label;
    }
    
    cell.name = name;
    if (self.showTpye == 1) {
        if ([self.province isEqualToString:name]) {
            cell.isSelected = YES;
        }else{
            cell.isSelected = NO;
        }
    }
    if (self.showTpye == 1) {
        if ([self.city isEqualToString:name]) {
            cell.isSelected = YES;
        }else{
            cell.isSelected = NO;
        }
    }
    
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *addressTitle = @"";
    
    id mo = self.showArray[indexPath.row];
    if ([mo isKindOfClass:[HAAddressProvinceMo class]]) {
        HAAddressProvinceMo *provinceModel = (HAAddressProvinceMo *)mo;
        addressTitle = provinceModel.label;
    }else if ([mo isKindOfClass:[HAAddressCityMo class]]) {
        HAAddressCityMo *cityModel = (HAAddressCityMo *)mo;
        addressTitle = cityModel.label;
    }else if ([mo isKindOfClass:[HAAddressAreaMo class]]) {
        HAAddressAreaMo *areaModel = (HAAddressAreaMo *)mo;
        addressTitle = areaModel.label;
    }

    if (self.showTpye == 1) {
        //进入城市选择
        HAAddressProvinceMo *provinceModel = self.showArray[indexPath.row];
        NSArray *arr3 = provinceModel.children;

        [self.showArray removeAllObjects];
        [self.showArray addObjectsFromArray:arr3];
        [self.tableView reloadData];

        if (arr3.count > 0) {
            HAAddressCityMo *city = arr3[0];
            NSString *text  = city.label;
            self.city = text;
            [self.selectedArray removeAllObjects];
            [self.selectedArray addObject:addressTitle];
            [self.selectedArray addObject:text];
            self.province = addressTitle;
        }
        [self.collectionView reloadData];
        self.showTpye = 2;

    }else if (self.showTpye == 2) {
        //进入地区选

        self.city = addressTitle;

        HAAddressCityMo *cityModel = self.showArray[indexPath.row];
        NSArray *arr4 = cityModel.children;

        [self.showArray removeAllObjects];
        [self.showArray addObjectsFromArray:arr4];
        [self.tableView reloadData];
        [self.selectedArray removeLastObject];
        [self.selectedArray addObject:addressTitle];
        [self.collectionView reloadData];
        self.showTpye = 3;

    }else if (self.showTpye == 3) {

        //选择完成
        self.area = addressTitle;
        if (self.HAAddressPopViewSureBlock) {
            self.HAAddressPopViewSureBlock(self.province, self.city, self.area);
        }
        [self dismiss];

    }
}


#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.selectedArray.count;
}

- (__kindof UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SYDZTalMenuColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SYDZTalMenuColCellID" forIndexPath:indexPath];
    NSString *name = self.selectedArray[indexPath.row];
    cell.name = name;
    return cell;
}


#pragma mark -- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(80,44);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *addressTitle = self.selectedArray[indexPath.row];
    if (indexPath.row == 0) {
        self.province = nil;
        self.city = nil;
        self.area = nil;
        NSArray *arr1 = self.dataArray;
        [self.showArray removeAllObjects];
        [self.showArray addObjectsFromArray:arr1];
        [self.selectedArray removeAllObjects];
        [self.selectedArray addObject:addressTitle];
        self.province = addressTitle;
        [self.collectionView reloadData];
        [self.tableView reloadData];
        self.showTpye = 1;
    }else if (indexPath.row == 1) {
    }
}

- (UIView *)topView1
{
    if (!_topView1) {
        _topView1 = [UIView ha_createView];
        _topView1.backgroundColor = [UIColor whiteColor];
        
    }
    return _topView1;
}

- (UIView *)topView2
{
    if (!_topView2) {
        _topView2 = [UIView ha_createView];
        _topView2.backgroundColor = [UIColor whiteColor];
        
    }
    return _topView2;
}

- (UIView *)lineView1
{
    if (!_lineView1) {
        _lineView1 = [UIView ha_createView];
        _lineView1.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        
    }
    return _lineView1;
}

- (UIView *)lineView2
{
    if (!_lineView2) {
        _lineView2 = [UIView ha_createView];
        _lineView2.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        
    }
    return _lineView2;
}

- (UIButton *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton setImage:[UIImage imageNamed:@"hl_close01"] forState:UIControlStateNormal];
        [_cancelButton setBackgroundColor:[UIColor whiteColor]];
        [_cancelButton addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (UILabel *)titleLael1
{
    if (!_titleLael1) {
        _titleLael1 = [[UILabel alloc]init];
        _titleLael1.text = @"请选择";
        _titleLael1.font = [UIFont systemFontOfSize:15];
        _titleLael1.textColor = [UIColor blackColor];
    }
    return _titleLael1;
}

- (UILabel *)titleLael2
{
    if (!_titleLael2) {
        _titleLael2 = [[UILabel alloc]init];
        _titleLael2.text = @"地址:";
        _titleLael2.font = [UIFont systemFontOfSize:13];
        _titleLael2.textColor = [UIColor colorWithHexString:@"#666666"];
    }
    return _titleLael2;
}


- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[SYDZTalMenuColCell class] forCellWithReuseIdentifier:@"SYDZTalMenuColCellID"];
    }
    return _collectionView;
}
- (NSMutableArray *)showArray
{
    if (!_showArray) {
        _showArray = [NSMutableArray array];
        
    }
    return _showArray;
}

- (NSMutableArray *)selectedArray
{
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
        
    }
    return _selectedArray;
}


@end
